package org.baseflexcharts.charts
{
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	
	import org.baseflexcharts.shapes.Legend;
	import org.baseflexcharts.shapes.Sector;
	
	public class PieChart extends Canvas
	{
		private var _dataProvider:ArrayCollection;
		
		private var _legendPosition:String = "right";
		
		private var _colors:Array = [0x0C3A0C, 0x0C4A0C, 0x0C5A0C, 0x0C6A0C, 0x0C7A0C, 0x0C8A0C, 0x0C9A0C, 0x0CaA0C, 0x0CbA0C, 0x0CcA0C, 0x0CdA0C];
		
		private var _baseColor:uint = 0x000000;
		
		private var legendData:ArrayCollection = new ArrayCollection();
		
		public static var TOP:String = "top";
		public static var LEFT:String = "left";
		public static var RIGHT:String = "right";
		public static var BOTTOM:String = "bottom";
		public static var NONE:String = "none";
		
		public function PieChart()
		{
			super();
		}
		
		public function draw():void
		{
			if (dataProvider == null)
			{
				return;
			}
			
			var radius:Number = 0;
			var centerX:Number = 0;
			var centerY:Number = 0;
			var totalValue:Number = 0;
			var startAngle:Number = 0;	
			
			if (this.legendPosition == PieChart.TOP || this.legendPosition == PieChart.BOTTOM)
			{
				radius = this.width / 2;
				centerX = this.width / 2;
				centerY = centerX;  
			}
			else
			{
				radius = this.height / 2;
				centerX = this.height / 2;
				centerY = centerX; 
			}
			
			for (var i:int = 0; i < dataProvider.length; i++)
			{
				var sectorData:Object = dataProvider.getItemAt(i);
				
				totalValue = totalValue + Number(sectorData.value);
			} 					
			
			
			for (var j:int = 0; j < dataProvider.length; j++)
			{
				var data:Object = dataProvider.getItemAt(j);
				var angle:Number = data.value / (totalValue / 360);			
				
			    var color:uint = this.colors[j];
				
				if (j == dataProvider.length -1)
				{
					angle = angle + 1;
				}
				
				var tip:String = data.name + " " + uint(data.value * 100 / totalValue).toString() + "%";
				
				var sector:Sector = new Sector(radius, startAngle, angle, tip, color, color, 1, 1);
			    sector.x = centerX;
			    sector.y = centerY;
			    
			    sector.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			    sector.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			    
			    this.addChild(sector);
			    
			    legendData.addItem({name: data.name, color: color});
			    
			    startAngle = startAngle + angle;
			}
			
			drawLegend();
		}
		
		private function drawLegend():void
		{
			var legend:Legend = new Legend();
			legend.dataProvider = this.legendData;
			legend.direction = Legend.VERTICAL;
			legend.type = Legend.TYPE_RECT;
			legend.draw();
			
			if (this.legendPosition == PieChart.TOP || this.legendPosition == PieChart.BOTTOM)
			{
				legend.x = 10;
			    legend.y = this.width + 10; 
			}
			else
			{
				legend.x = this.height + 10;
			    legend.y = 10;
			}			
			
			this.addChild(legend);
		}
		
		private function mouseOverHandler(event:MouseEvent):void
		{
			var sector:Sector = Sector(event.currentTarget);
			
			sector.radius = sector.radius + 2;
			if (sector.startAngle > 90 && sector.startAngle < 270)
			{
				sector.x = sector.x - 2;
				sector.y = sector.y - 2;
			}
			else
			{
				sector.x = sector.x + 2;
				sector.y = sector.y + 2;
			}
			sector.draw();
		}
		
		private function mouseOutHandler(event:MouseEvent):void
		{
			var sector:Sector = Sector(event.currentTarget);
			
			sector.radius = sector.radius - 2;
			if (sector.startAngle > 90 && sector.startAngle < 270)
			{
				sector.x = sector.x + 2;
				sector.y = sector.y + 2;
			}
			else
			{
				sector.x = sector.x - 2;
				sector.y = sector.y - 2;
			}
			sector.draw();
		}
		
		public function get dataProvider():ArrayCollection
		{
			return this._dataProvider;
		}
        
        public function set dataProvider(data:ArrayCollection):void
        {
        	this._dataProvider = data;
        }  
        
        public function get legendPosition():String
		{
			return this._legendPosition;
		}
        
        public function set legendPosition(data:String):void
        {
        	this._legendPosition = data;
        }
        
        public function get baseColor():uint
		{
			return this._baseColor;
		}
        
        public function set baseColor(data:uint):void
        {
        	this._baseColor = data;
        }
        
        public function get colors():Array
		{
			return this._colors;
		}
        
        public function set colors(data:Array):void
        {
        	this._colors = data;
        }
	}
}